﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using IWshRuntimeLibrary;

namespace LEET.Application
{
    [RunInstaller(true)]
    public class CustomShortcuts : Installer
    {
        private string QuickLaunchFolder
        {
            get
            {
                return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Microsoft\\Internet Explorer\\Quick Launch";
            }
        }

        public override void Install(IDictionary savedState)
        {
            base.Install(savedState);

            const string ALLUSERS_PARAM = "ALLUSERS";
            if (!Context.Parameters.ContainsKey(ALLUSERS_PARAM))
                throw new Exception(string.Format(
                  "The {0} parameter has not been provided for the {1} class.",
                  ALLUSERS_PARAM, this.GetType()));

            const string DESKTOP_SHORTCUT_PARAM = "DESKTOP_SHORTCUT";
            if (!Context.Parameters.ContainsKey(DESKTOP_SHORTCUT_PARAM))
                throw new Exception(string.Format(
                  "The {0} parameter has not been provided for the {1} class.",
                  ALLUSERS_PARAM, this.GetType()));

            const string QUICKLAUNCH_SHORTCUT_PARAM = "QUICKLAUNCH_SHORTCUT";
            if (!Context.Parameters.ContainsKey(QUICKLAUNCH_SHORTCUT_PARAM))
                throw new Exception(string.Format(
                  "The {0} parameter has not been provided for the {1} class.",
                  ALLUSERS_PARAM, this.GetType()));

            bool allusers = Context.Parameters[ALLUSERS_PARAM] != string.Empty;
            bool installDesktopShortcut = Context.Parameters[DESKTOP_SHORTCUT_PARAM] != string.Empty;
            bool installQuickLaunchShortcut = Context.Parameters[QUICKLAUNCH_SHORTCUT_PARAM] != string.Empty;

            string desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            object allUsersDesktop = "AllUsersDesktop";
            WshShell shell = new WshShell();
            desktopFolder = shell.SpecialFolders.Item(ref allUsersDesktop).ToString();
        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);

        }

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);

        }

        private void CreateShortcut(string folder, string name, string target, string description)
        {
            string shortcutFullName = Path.Combine(folder, name + ".lnk");

            try
            {
                WshShell shell = new WshShell();
                IWshShortcut link =
                  (IWshShortcut)shell.CreateShortcut(shortcutFullName);
                link.TargetPath = target;
                link.Description = description;
                link.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("The shortcut \"{0}\" could not be created.\n\n{1}", shortcutFullName, ex.ToString()), "Create Shortcut", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void DeleteShortcut(string folder, string name)
        {
            string shortcutFullName = Path.Combine(folder, name + ".lnk");
            FileInfo shortcut = new FileInfo(shortcutFullName);
            if (shortcut.Exists)
            {
                try
                {
                    shortcut.Delete();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(string.Format("The shortcut \"{0}\" could not be deleted.\n\n{1}", shortcutFullName, ex.ToString()), "Delete Shortcut", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
}
